package com.ruoyi.basedata.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.basedata.domain.OmsOrgunit;
import com.ruoyi.basedata.domain.OmsSettlementrateentrytwo;
import com.ruoyi.basedata.service.IOmsOrgunitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basedata.domain.OmsSettlementrateentry;
import com.ruoyi.basedata.mapper.OmsSettlementrateMapper;
import com.ruoyi.basedata.domain.OmsSettlementrate;
import com.ruoyi.basedata.service.IOmsSettlementrateService;

/**
 * 结算加成比率Service业务层处理
 *
 * @author ptt
 * @date 2023-02-08
 */
@Service
public class OmsSettlementrateServiceImpl implements IOmsSettlementrateService {
    @Autowired
    private OmsSettlementrateMapper omsSettlementrateMapper;

    @Autowired
    private IOmsOrgunitService omsOrgunitService;

    /**
     * 查询结算加成比率
     *
     * @param id 结算加成比率主键
     * @return 结算加成比率
     */
    @Override
    public OmsSettlementrate selectOmsSettlementrateById(Long id) {
        return omsSettlementrateMapper.selectOmsSettlementrateById(id);
    }

    /**
     * 查询结算加成比率列表
     *
     * @param omsSettlementrate 结算加成比率
     * @return 结算加成比率
     */
    @Override
    public List<OmsSettlementrate> selectOmsSettlementrateList(OmsSettlementrate omsSettlementrate) {
        return omsSettlementrateMapper.selectOmsSettlementrateList(omsSettlementrate);
    }
    @Override
    public List<Long> selectOmsSettlementrateIdList(OmsSettlementrate omsSettlementrate) {
        return omsSettlementrateMapper.selectOmsSettlementrateIdList(omsSettlementrate);
    }

    @Override
    public List<OmsSettlementrate> selectOmsSettlementrateAndEntryList(List<Long> ids) {
        return omsSettlementrateMapper.selectOmsSettlementrateAndEntryList(ids);
    }

    /**
     * 新增结算加成比率
     *
     * @param omsSettlementrate 结算加成比率
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSettlementrate(OmsSettlementrate omsSettlementrate) {
        int i = primecostSuccess(omsSettlementrate, true);
        if (i != 1) {
            return i;
        }
        int rows = omsSettlementrateMapper.insertOmsSettlementrate(omsSettlementrate);
        insertOmsSettlementrateentry(omsSettlementrate);
        insertOmsSettlementrateentrytwo(omsSettlementrate);
        return rows;
    }

    /**
     * 修改结算加成比率
     *
     * @param omsSettlementrate 结算加成比率
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSettlementrate(OmsSettlementrate omsSettlementrate) {
        int i = primecostSuccess(omsSettlementrate, true);
        if (i != 1) {
            return i;
        }
        omsSettlementrateMapper.deleteOmsSettlementrateentryByParentId(omsSettlementrate.getId());
        omsSettlementrateMapper.deleteOmsSettlementrateentrytwoByParentId(omsSettlementrate.getId());
        insertOmsSettlementrateentry(omsSettlementrate);
        insertOmsSettlementrateentrytwo(omsSettlementrate);
        return omsSettlementrateMapper.updateOmsSettlementrate(omsSettlementrate);
    }

    /**
     * 批量删除结算加成比率
     *
     * @param ids 需要删除的结算加成比率主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSettlementrateByIds(Long[] ids) {
        omsSettlementrateMapper.deleteOmsSettlementrateentryByParentIds(ids);
        omsSettlementrateMapper.deleteOmsSettlementrateByIds(ids);
        return omsSettlementrateMapper.deleteOmsSettlementrateentrytwoByParentIds(ids);
    }

    /**
     * 删除结算加成比率信息
     *
     * @param id 结算加成比率主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSettlementrateById(Long id) {
        omsSettlementrateMapper.deleteOmsSettlementrateentryByParentId(id);
        omsSettlementrateMapper.deleteOmsSettlementrateById(id);
        return omsSettlementrateMapper.deleteOmsSettlementrateentrytwoByParentId(id);
    }

    /**
     * 新增结算加成比率分录信息
     *
     * @param omsSettlementrate 结算加成比率对象
     */
    public void insertOmsSettlementrateentry(OmsSettlementrate omsSettlementrate) {
        List<OmsSettlementrateentry> omsSettlementrateentryList = omsSettlementrate.getOmsSettlementrateentryList();
        Long id = omsSettlementrate.getId();
        if (StringUtils.isNotNull(omsSettlementrateentryList)) {
            List<OmsSettlementrateentry> list = new ArrayList<OmsSettlementrateentry>();
            for (OmsSettlementrateentry omsSettlementrateentry : omsSettlementrateentryList) {
                omsSettlementrateentry.setParentid(id);
                list.add(omsSettlementrateentry);
            }
            if (list.size() > 0) {
                omsSettlementrateMapper.batchOmsSettlementrateentry(list);
            }
        }
    }


    public void insertOmsSettlementrateentrytwo(OmsSettlementrate omsSettlementrate) {
        List<OmsSettlementrateentrytwo> OmsSettlementrateentrytwos = omsSettlementrate.getOmsSettlementrateentrytwoList();
        Long id = omsSettlementrate.getId();
        if (StringUtils.isNotNull(OmsSettlementrateentrytwos)) {
            ArrayList<OmsSettlementrateentrytwo> list = new ArrayList<>();
            for (OmsSettlementrateentrytwo OmsSettlementrateentrytwo : OmsSettlementrateentrytwos) {
                OmsSettlementrateentrytwo.setParentid(id);
                list.add(OmsSettlementrateentrytwo);
            }
            if (list.size() > 0) {
                omsSettlementrateMapper.batchOmsSettlementrateentrytwo(list);
            }
        }
    }

    private int primecostSuccess(OmsSettlementrate omsSettlementrate, boolean isAdd) {
        String saleorgnumber = omsSettlementrate.getSaleorgnumber();
        if(omsSettlementrate.getSaleorgnumber()==null){
            return -3;
        }
        OmsOrgunit omsOrgunit = new OmsOrgunit();
        omsOrgunit.setOrgnumber(saleorgnumber);
        omsOrgunit.setOrgname(omsSettlementrate.getSaleorgname());
        List<OmsOrgunit> omsOrgunitlist = omsOrgunitService.selectOmsOrgunitList(omsOrgunit);
        if(omsOrgunitlist==null||omsOrgunitlist.size()==0){
            return -4;
        }
        List<OmsSettlementrateentry> omsSetList = omsSettlementrate.getOmsSettlementrateentryList();
        List<OmsSettlementrateentrytwo> omsSetTwoList = omsSettlementrate.getOmsSettlementrateentrytwoList();
        if(omsSettlementrate.getStatus()==1L&&
                (omsSetTwoList==null||omsSetTwoList.size()==0
                ||omsSetList==null||omsSetList.size()==0)){
            return -2;
        }
        for (OmsSettlementrateentry omsSet : omsSetList) {
            String pricetype = omsSet.getPricetype();
            if (omsSettlementrate.getStatus()==1L&&"7".equals(pricetype)
                    && (StringUtils.isBlank(omsSet.getMaterialnumber())
                    || !StringUtils.isBlank(omsSet.getCategorynumber()))) {
                return -1;
            }else if(omsSettlementrate.getStatus()==1L&&!"7".equals(pricetype)
                    && (!StringUtils.isBlank(omsSet.getMaterialnumber())
                    || StringUtils.isBlank(omsSet.getCategorynumber()))) {
                return -1;
            }
        }
        return 1;
    }
}
